package com.youyicloud.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.codec.binary.Base64;

public class Base64Util {
	
	private static String base64Str = "iVBORw0KGgoAAAANSUhEUgAAADYAAAA2CAYAAACMRWrdAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTM4IDc5LjE1OTgyNCwgMjAxNi8wOS8xNC0wMTowOTowMSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIDIwMTcgKFdpbmRvd3MpIiB4bXBNTTpJbnN0YW5jZUlEPSJ4bXAuaWlkOkNBMkI3NjMxNDY5QzExRThCMkUyRDBFQjQ0MzUwRDRDIiB4bXBNTTpEb2N1bWVudElEPSJ4bXAuZGlkOkNBMkI3NjMyNDY5QzExRThCMkUyRDBFQjQ0MzUwRDRDIj4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmV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FLAy5IZ6nlKFTGO0Xlrr7mjNO9bk0a0zhYTUnXz5+wHJpww1hSDOPw2ZHZ2e1+g6VRxud3r5W0i+0E3x2M9jZ3QWffCYlDeF/tRirdwruekY9Centb4eFivH0tufVsdO/jsbZpMVACXi9nGkhKTPwIGSZ4gVPbQOU29HVk5v/Um7rM4p29CK9MRl+TG+kCOpVK026vq32APVG+8LXfkpzd/XSb8TM9M6DcntbhVN57vk/9EbMz/PFj/HZunX2jyVgP+TxBbGSx/K32KDl1gax5cNfCDAAC82Ma8Kz2fsAAAAASUVORK5CYII="; 
	
	public static void main(String[] args) {
		GenerateImage(base64Str, "D:\\test.jpg");
	}
	
	public String GetImageStr()    
    {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理    
        String imgFile = "d://test.jpg";//待处理的图片    
        InputStream in = null;    
        byte[] data = null;    
        //读取图片字节数组    
        try     
        {    
            in = new FileInputStream(imgFile);            
            data = new byte[in.available()];    
            in.read(data);    
            in.close();    
        }     
        catch (IOException e)     
        {    
            e.printStackTrace();    
        }    
        //对字节数组Base64编码    
        Base64 encoder = new Base64();    
        return encoder.encodeAsString(data);//返回Base64编码过的字节数组字符串    
    }  
      
    //base64字符串转化成图片  
    public static boolean GenerateImage(String imgStr,String imgFilePath)  
    {   //对字节数组字符串进行Base64解码并生成图片  
        if (imgStr == null) //图像数据为空  
            return false;  
        Base64 decoder = new Base64();    
        try   
        {  
            //Base64解码  
            byte[] b = decoder.decodeBase64(imgStr);
            for(int i=0;i<b.length;++i)  
            {  
                if(b[i]<0)  
                {//调整异常数据  
                    b[i]+=256;  
                }  
            }  
            //生成jpeg图片    
            OutputStream out = new FileOutputStream(imgFilePath);      
            out.write(b);  
            out.flush();  
            out.close();  
            return true;  
        }   
        catch (Exception e)   
        {  
            return false;  
        }  
    }
}
